package com.blink.curator.operate;

import com.blink.base.BaseTestSample;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.junit.jupiter.api.Test;

/**
 * <Description> 使用Curator更新数据内容</Description>
 * <ClassName> SetDataSample</ClassName>
 *
 * @Author liuxianzhao
 * @Date 2018年01月13日 15:14
 */
public class SetDataSample extends BaseTestSample {
    @Test
    public void testSetData() throws Exception {
        CuratorFramework client = getZkCurator();
        client.start();
        String path = "/zk-book";
//        client.delete()
//                .deletingChildrenIfNeeded()
//                .forPath(path);
        client.create()
                .creatingParentsIfNeeded()
                .withMode(CreateMode.EPHEMERAL)
                .forPath(path, "init".getBytes());
        Stat stat = new Stat();
        client.getData().storingStatIn(stat).forPath(path);
        logger.info("Success set node for : " + path + ", new version: "
                + client.setData().withVersion(stat.getVersion()).forPath(path).getVersion());
        try {
            client.setData().withVersion(stat.getVersion()).forPath(path);
        } catch (Exception e) {
            logger.error("Fail set node due to ", e);
        }
        sleep();
    }
}
